package com.core.sgip.common.body.command;

import org.apache.log4j.Logger;

import com.core.sgip.common.SGIPUtils;
import com.core.sgip.common.body.SGIPCommand;

public class Report extends SGIPCommand {

	private static Logger logger = Logger.getLogger(Report.class);

	private static final int COMMAND_LENGTH = 44;

	private byte[] submitSequenceNumber = new byte[12];// 12

	private String submitSequenceNumberStr;

	public String getSubmitSequenceNumberStr() {
		byte[] a = new byte[4];
		byte[] b = new byte[4];
		byte[] c = new byte[4];

		SGIPUtils.copyBytes(this.submitSequenceNumber, a, 1, 4, 1);
		SGIPUtils.copyBytes(this.submitSequenceNumber, b, 5, 4, 1);
		SGIPUtils.copyBytes(this.submitSequenceNumber, c, 9, 4, 1);

		return String.valueOf(SGIPUtils.byte4ToLong(a)) + String.valueOf(SGIPUtils.byte4ToLong(b))
				+ String.valueOf(SGIPUtils.byte4ToLong(c));
	}

	
	public void setSubmitSequenceNumber(Long a,Long b,Long c) {
		byte [] abte =SGIPUtils.convertLong2FourBytes(a);
		byte [] bbyte = SGIPUtils.convertLong2FourBytes(b);
		byte [] cbyte = SGIPUtils.convertLong2FourBytes(c);
		this.submitSequenceNumber = SGIPUtils.mergeBytes(abte,bbyte,cbyte);
		
		byte[] aa = new byte[4];
		byte[] bb = new byte[4];
		byte[] cc = new byte[4];

		SGIPUtils.copyBytes(this.submitSequenceNumber, aa, 1, 4, 1);
		SGIPUtils.copyBytes(this.submitSequenceNumber, bb, 5, 4, 1);
		SGIPUtils.copyBytes(this.submitSequenceNumber, cc, 9, 4, 1);
		
		this.submitSequenceNumberStr = String.valueOf(SGIPUtils.byte4ToLong(abte)) + String.valueOf(SGIPUtils.byte4ToLong(bbyte))
				+ String.valueOf(SGIPUtils.byte4ToLong(cbyte));
	}

	private int reportType;

	private String userNumber;

	private int state;// 0：发送成功 1：等待发送 2：发送失败

	private int errorCode;
	private String reservestr;

	private byte[] reserve = new byte[8];

	private byte[] reportTypeByte = new byte[1];

	private byte[] stateByte = new byte[1];

	private byte[] errorCodeByte = new byte[1];

	private byte[] userNumberByte = new byte[21];




	public int getReportType() {
		return reportType;
	}

	public void setReportType(int reportType) {
		this.reportType = reportType;
	}

	public String getUserNumber() {
		return userNumber;
	}

	public void setUserNumber(String userNumber) {
		this.userNumber = userNumber;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public Report() {
		super();
		this.commandLength = COMMAND_LENGTH;
	}

	private void calcute() {

	}

	public String getReservestr() {
		return reservestr;
	}

	public void setReservestr(String reservestr) {
		this.reservestr = reservestr;
	}

	@Override
	public byte[] getByteData() {
		calcute();
		convertProperties2bytes();
		return SGIPUtils.mergeBytes(this.submitSequenceNumber, this.reportTypeByte, this.userNumberByte, this.stateByte,this.errorCodeByte,this.reserve);
	}
	public byte[] getByteDatareport(String seq) {
		convertProperties2bytes(seq);
		return SGIPUtils.mergeBytes(this.submitSequenceNumber, this.reportTypeByte, this.userNumberByte, this.stateByte,this.errorCodeByte,this.reserve);
	}
	
	private void convertProperties2bytes(String seq) {
		setSubmitSequenceNumber(Long.valueOf(seq.substring(0, 10)), Long.valueOf(seq.substring(10, (seq.length() - 1))),
				Long.valueOf(String.valueOf(seq.charAt(seq.length() - 1))));

		this.reportTypeByte[0] = SGIPUtils.convertInt2Byte(this.reportType);

		byte[] pwdSource = this.userNumber.getBytes();
		int pwdLength = pwdSource.length;
		SGIPUtils.copyBytes(pwdSource, this.userNumberByte, 1, pwdLength, 1);

		this.stateByte[0] = SGIPUtils.convertInt2Byte(this.state);
		this.errorCodeByte[0] = SGIPUtils.convertInt2Byte(this.errorCode);

	}
	private void convertProperties2bytes() {
		// 待优化
		// byte[] source = this.submitSequenceNumberStr.getBytes();
		// int length = source.length;
		// SGIPUtils.copyBytes(source, this.submitSequenceNumber, 1, length, 1);
		
		this.reportTypeByte[0] = SGIPUtils.convertInt2Byte(this.reportType);
		
		byte[] pwdSource = this.userNumber.getBytes();
		int pwdLength = pwdSource.length;
		SGIPUtils.copyBytes(pwdSource, this.userNumberByte, 1, pwdLength, 1);
		
		this.stateByte[0] = SGIPUtils.convertInt2Byte(this.state);
		this.errorCodeByte[0] = SGIPUtils.convertInt2Byte(this.errorCode);
		
	}
	@Override
	public void initPropertiesByBytes(byte[] source) {
		if (source != null && source.length >= COMMAND_LENGTH-8) {
			SGIPUtils.copyBytes(source, this.submitSequenceNumber, 1, 12, 1);
			SGIPUtils.copyBytes(source, this.reportTypeByte, 13, 1, 1);
			SGIPUtils.copyBytes(source, this.userNumberByte, 14, 21, 1);
			SGIPUtils.copyBytes(source, this.stateByte, 35, 1, 1);
			SGIPUtils.copyBytes(source, this.errorCodeByte, 36, 1, 1);

			this.reportType = SGIPUtils.convertUnsignByte2Int(this.reportTypeByte[0]);
			this.userNumber = new String(this.userNumberByte);
			this.state = SGIPUtils.convertUnsignByte2Int(this.stateByte[0]);
			this.errorCode = SGIPUtils.convertUnsignByte2Int(this.errorCodeByte[0]);
		} else {
			logger.error("init Report properties from bytes error: the bytes not enough");
		}
	}

	@Override
	public String toString() {
		return "Report [submitSequenceNumberStr=" + getSubmitSequenceNumberStr() + ", reportType=" + reportType
				+ ", userNumber=" + userNumber + ", state=" + state + ", errorCode=" + errorCode + "]";
	}

}
